<?php

namespace App\Jobs;

use App\Models\User\WxUser;
use App\Wen\Utils\UserUtils;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ImitateUserFollowsStepJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $tries = 1;
    protected $user_id = 0;
    protected $need_follow = 0;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($user_id, $need_follow)
    {
        $this->user_id = $user_id;
        $this->need_follow = $need_follow;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        if( $this->need_follow <= 0 ){
            return;
        }

        global $__MINI_GLOBAL_REQUEST_ENV__;
        $__MINI_GLOBAL_REQUEST_ENV__ = 'api';

        if(WxUser::where('id', $this->user_id)->value('user_state') != 0){
            return;
        }
        $uid = UserUtils::get_a_random_robot_uid();
        if($uid){

            global $__MINI_GLOBAL_IS_ADMIN_SUPER__;
            if($this->user_id > 0 && in_array($this->user_id, explode(',', env('APP_SUPER_ADMIN_USER_ID', '')))){
                $__MINI_GLOBAL_IS_ADMIN_SUPER__ = true;
            }
            $res = UserUtils::follow_user($uid, $this->user_id);
            $__MINI_GLOBAL_IS_ADMIN_SUPER__ = false;

            if($res['code'] == 1){

            }
            ImitateUserFollowsStepJob::dispatch($this->user_id, ($this->need_follow - 1))->delay(now()->addMinutes(mini_rand(6, 30)));;
        }
    }
}
